package com.spic.business.train.trainRecord.service.impl;

import com.spic.business.common.core.utils.poi.BusinessExcelUtil;
import com.spic.business.train.courseware.service.ITrainCoursewareService;
import com.spic.business.train.files.domain.TrainInfoFiles;
import com.spic.business.train.files.service.ITrainInfoFilesService;
import com.spic.business.train.plan.domain.TrainPlan;
import com.spic.business.train.plan.domain.TrainPlanShift;
import com.spic.business.train.plan.domain.TrainPlanStaff;
import com.spic.business.train.plan.service.ITrainPlanService;
import com.spic.business.train.trainInfo.domain.TrainInfo;
import com.spic.business.train.trainInfo.service.ITrainInfoService;

import com.spic.business.train.trainRecord.service.ITrainRecordService;
import com.spic.business.train.trainSign.domain.TrainSign;
import com.spic.business.train.trainSign.service.ITrainSignService;
import com.spic.business.train.trainUser.domain.TrainUser;
import com.spic.business.train.trainUser.service.ITrainUserService;
import com.spic.business.utils.ZIPUtil;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.utils.UUIDUtil;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.system.api.RemoteFileService;
import com.spic.system.api.dto.DownloadFileDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ZipUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class TrainRecordService implements ITrainRecordService {
    @Autowired
    private ITrainPlanService trainPlanService;
    @Autowired
    private ITrainInfoService trainInfoService;
    @Autowired
    private ITrainSignService trainSignService;
    @Autowired
    private ITrainUserService trainUserService;
    @Autowired
    private ITrainCoursewareService trainCoursewareService;
    @Autowired
    private ITrainInfoFilesService trainInfoFilesService;
    @Autowired
    private RemoteFileService remoteFileService;



    @Value("${zip.path}")
    private String path;
    @Override
    public void export(HttpServletResponse response, String id) throws IOException {
        TrainInfo trainInfo=trainInfoService.selectTrainInfoById(id);

        List<String> urls = new ArrayList<>();
        path=path+"/"+ SecurityUtils.getUserId();
        //导出培训计划临时文件
        List<TrainPlan> list = new ArrayList<>();
        TrainPlan trainPlan=trainPlanService.selectTrainPlanById(trainInfo.getPlanId());
        list.add(trainPlan);
        ExcelUtil<TrainPlan> util = new BusinessExcelUtil<>(TrainPlan.class);
        urls.add(util.exportExcel(path, list, "培训计划数据"));
        //导出参培人员
        ExcelUtil<TrainPlanStaff> util1 = new BusinessExcelUtil<>(TrainPlanStaff.class);
        urls.add(util1.exportExcel(path, trainPlan.getTrainPlanStaffs(), "参培人数"));
        //导出班级
        ExcelUtil<TrainPlanShift> util2 = new BusinessExcelUtil<>(TrainPlanShift.class);
        urls.add(util2.exportExcel(path, trainPlan.getTrainPlanShiftList(), "培训班级"));
        //导出签到记录
        ExcelUtil<TrainSign> util3 = new BusinessExcelUtil<>(TrainSign.class);
        urls.add(util3.exportExcel(path, trainSignService.selectTrainSignByTrainInfoId(id), "签到记录"));
        //导出考核记录
        TrainUser trainUser=new TrainUser();
        trainUser.setTrainInfoId(id);
        ExcelUtil<TrainUser> util4 = new BusinessExcelUtil<>(TrainUser.class);
        urls.add(util4.exportExcel(path,trainUserService.selectTrainUserList(trainUser) , "考核记录"));
        //导出课件
        String coursewareFileName=path+"/课件.zip";
        if(trainPlan.getTrainCoursewareList() != null) {
            List<String> coursewareIds= Arrays.asList(trainPlan.getTrainCoursewareId().split(","));
            remoteFileService.downloadfilelistCache(new DownloadFileDTO(trainCoursewareService.selectUrlByIds(coursewareIds),coursewareFileName));
            urls.add(coursewareFileName);
        }
        //过程附件
        String trainFileName1=path+"/过程附件.zip";
        TrainInfoFiles trainInfoFiles=new TrainInfoFiles();
        trainInfoFiles.setTrainInfoId(id);
        trainInfoFiles.setFileClassification(0);
        remoteFileService.downloadfilelistCache(new DownloadFileDTO(trainInfoFilesService.selectTrainInfoFilesUrlList(trainInfoFiles),trainFileName1));
        urls.add(trainFileName1);
        //培训图片
        String trainFileName2=path+"/培训图片.zip";
        TrainInfoFiles trainInfoFiles1=new TrainInfoFiles();
        trainInfoFiles1.setTrainInfoId(id);
        trainInfoFiles1.setFileClassification(1);
        remoteFileService.downloadfilelistCache(new DownloadFileDTO(trainInfoFilesService.selectTrainInfoFilesUrlList(trainInfoFiles1),trainFileName2));
        urls.add(trainFileName2);
        //打包下载
        ZIPUtil.downfilelist(response,urls,path);


    }
}
